package com.bridgeQQ.client.control
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.describeType;
	
	import uitl.DisplayUtil;
	
	public class BaseContextMenuManager
	{
		protected var root:InteractiveObject;
		
		private var def:XML;
		
		private var rootMenu:NativeMenu;
		
		private var displayMenuItems:Array = [];
		
		public function BaseContextMenuManager(root:InteractiveObject, def:XML)
		{
			this.root = root;
			this.def = def;
			this.root.addEventListener(MouseEvent.RIGHT_MOUSE_DOWN, onContextMenuShow);
		}
		
		private function onContextMenuShow(event:Event):void
		{
			createMenu();
		}
		
		private function createMenu():void
		{
			removeMenu();
			var allChildren:Array = [];
			DisplayUtil.getObjectsUnderPoint(root, new Point(root.stage.mouseX, root.stage.mouseY), allChildren);
			var length:int = allChildren.length;
			var defs:XMLList = def.display;
			for(var i:int = length - 1; i >= 0; i--)
			{
				var child:InteractiveObject = allChildren[i];
				var className:String = getClassName(child);
				if(displayMenuItems[className] != null && displayMenuItems[className].length > 0)
				{
					continue;
				}
				var items:XMLList = defs.(@["class"] == className).menu_item;
				var itemsLength:int = items.length();	
				if(itemsLength <= 0)
				{
					continue;
				}
				var display:XML = items.parent()[0];
				if(display.@childChain[0] != null && child is DisplayObjectContainer)
				{
					var childName:String = display.@childChain[0];
					var childDisplay:DisplayObject = getChainChild(DisplayObjectContainer(child), childName);
					if(allChildren.indexOf(childDisplay) < 0)
					{
						continue;
					}
				}
				var labels:Array = [];
				displayMenuItems[className] = labels;
				displayMenuItems["display" + className] = child;		
				if(rootMenu == null)
				{
					rootMenu = new NativeMenu();
				}
				for(var k:int = 0; k < itemsLength; k++)
				{
					var menuItem:NativeMenuItem = createMenuItem(child, items[k]);
					if(menuItem != null)
					{
						labels.push(menuItem.label);	
						rootMenu.addItem(menuItem);
						menuItem.addEventListener(Event.SELECT, onMenuSelect);
					}
				}
			}	
			root.contextMenu = rootMenu;
		}
		
		private function onMenuSelect(event:Event):void
		{
			var label:String = event.currentTarget.label;
			var display:DisplayObject;
			for (var className:String in displayMenuItems)
			{
				var items:Array= displayMenuItems[className] as Array;
				if(items != null && items.indexOf(label) >= 0)
				{
					display = displayMenuItems["display" + className];
					break;
				}
			}
			menuItemSelected(display, label);
		}
		
		
		private function removeMenu():void
		{
			displayMenuItems = [];
			if(rootMenu)
				rootMenu.removeAllItems();
			root.contextMenu = null;
		}
		
		protected function createMenuItem(obj:DisplayObject, def:XML):NativeMenuItem
		{
			return null;
		}
		
		protected function menuItemSelected(obj:DisplayObject, label:String):void
		{
			
		}
		
		
		private function getChainChild(parent:DisplayObjectContainer, chain:String):DisplayObject
		{
			var index:int = chain.indexOf(".");
			var name:String = index > 0 ? chain.substring(0, index) : chain;
			var obj:DisplayObject = parent.getChildByName(name);
			if(index > 0)
			{
				var remaing:String = chain.substring(index + 1);
				if(remaing.length > 0 && obj is DisplayObjectContainer)
				{
					return getChainChild(DisplayObjectContainer(obj), remaing);
				}
			}
			return obj;
		}
		
		private function getClassName(obj:Object):String
		{
            var name:String= describeType(obj).@name;
            name = name.replace("::",".");
			return name;
		}
		
		
	}
}